package mail.owner;

import java.sql.ResultSet;
import java.sql.SQLException;

import mail.db.DataBase;

public class Owner {
	public static final String TABLE = "owners";
	public static final String ID = "id";
	public static final String NAME = "name";
	public static final String EMAIL = "email";
	
	private DataBase db;
	
	private int id;
	private String name;
	private String email;
	
	public Owner(int id) {
		
	}
	
	public Owner(String email) throws SQLException {
		this(email, email);
	}
	
	public Owner(String name, String email) throws SQLException {
		this(name, email, DataBase.getInstance());
	}
	
	private Owner(String name, String email, DataBase db) throws SQLException {
		this.name = name;
		this.email = email;
		this.db = db;

		ResultSet rs = db.executeQuery("SELECT * FROM "+TABLE+" WHERE "+EMAIL+"=\""+email+"\"");
		try {
			System.out.println("Searching for owner ("+name+", "+email+")");
			rs.first();
			id = rs.getInt(ID);
			System.out.println("Owner ("+name+", "+email+") found @ id = "+id);
		} catch (SQLException e) {
			System.out.println("Owner name ("+name+", "+email+") does not exist, adding to database...");
			id = db.executeUpdate("INSERT INTO "+TABLE+" ("+NAME+","+EMAIL+") VALUES (\""+name+"\",\""+email+"\")");
		}
	}
	
	
	public int getID() { return id; }
	public String getName() { return name; }
	public String getEmail() { return email; }
}
